﻿[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.Additions.Addition.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.Additions.Addition.debug.js", "application/x-javascript")]

namespace BM.Tools.WebControls.MForm.Controls.Additions
{
    using System;
    using System.ComponentModel;        
    using System.Web.UI;

    using AjaxControlToolkit;

    using BM.Tools.WebControls.MForm.Controls.Items;
    using BM.Tools.WebControls.MForm.ScriptReferences;

    using Helper;

    /// <summary>
    /// The addition control can be attached to the item to provide some extra functionality both on server side, as well as on client side.
    /// Creating the addition on server side requires specifying the properties that will be sent to the client.
    /// Specifying these properties is done with the use of the <see cref="AttributePropertyAttribute" /> attribute and is provided in html using non-standard attributes. 
    /// The <see cref="Addition" /> is similiar to the idea of the ASP.NET AJAX extender. The difference is that extenders are coupled to a given html element statically,
    /// whereas MForm items can be created dynamically on client side.
    /// All item's additions are initiated on item creation.
    /// The additions reside in the <see cref="AdditionsDataControl"/> control which is a child control of the item.    
    /// </summary>
    [DesignTimeVisible(false)]
    [ParseChildren(true)]
    [ClientScriptResource("BM.Tools.WebControls.MForm", "BM.Tools.WebControls.MForm.Controls.Additions.Addition.js")]
    public abstract class Addition : Control
    {
        /// <summary>
        /// Gets or sets the informative text about the addition.
        /// </summary>        
        [AttributeProperty(Name = "infoText")]
        [Localizable(true)]
        public virtual string InfoText
        {
            get;
            set;
        }

        /// <summary>
        /// Gets TagName.
        /// </summary>
        /// <value>
        /// The tag name.
        /// </value>
        public virtual string TagName
        {
            get
            {
                return "b";
            }
        }

        /// <summary>
        /// Gets the type name.
        /// </summary>
        /// <value>
        /// The type name.
        /// </value>
        [AttributeProperty(Name = "class")]
        public string TypeName
        {
            get
            {
                return GetType().Name;
            }
        }

        /// <summary>
        /// Gets the offspring item to which the addition is bound        
        /// </summary>
        protected OffspringItem Owner
        {
            get
            {
                return Parent.Parent as OffspringItem;
            }
        }           

        /// <summary>
        /// does the standard OnPreRender and includes resources
        /// </summary>
        /// <param name="e">
        /// The event args.
        /// </param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            ScriptIncluder.IncludeResources(this);
        }

        /// <summary>
        /// custom render behavior
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected override void Render(HtmlTextWriter writer)
        {
            this.AddAttributesFromProperties(writer);
            writer.RenderBeginTag(TagName);
            RenderChildren(writer);
            this.AddContentFromProperties(writer);
            writer.RenderEndTag();
        }        
    }
}